package com.feign;

import com.domain.UserInfo;
import com.exception.BusinessException;
import com.exception.MemberCodeMsg;
import com.service.IUserInfoService;
import com.util.AssertUtils;
import com.util.JsonResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class UserInfoFeignClient implements UserInfoFeignApi {
    @Autowired
    private IUserInfoService userInfoService;
    @Override
    public JsonResult get(String id) {
        return JsonResult.success(userInfoService.get(id));
    }

    @Override
    public JsonResult checkPhone(String phone) {
        return JsonResult.success(userInfoService.checkPhone(phone));
    }

    @Override
    public JsonResult regist(String phone, String nickname, String password, String rpassword) {
        //校验参数是否为空
        AssertUtils.hasLength(phone , MemberCodeMsg.PHONE_ERROR);
        AssertUtils.hasLength(nickname , MemberCodeMsg.NICKNAME_ERROR);
        AssertUtils.hasLength(password , MemberCodeMsg.PASSWORD_ERROR);
        AssertUtils.hasLength(rpassword , MemberCodeMsg.REPASSWORD_ERROR);
        //校验两次密码是否相等
        AssertUtils.isEquals(password,rpassword,MemberCodeMsg.PASSWORD_EQUAL_ERROR);
        //校验手机号码是否正确 @Todo java的正则表达式
        //校验手机号是否唯一
        if (!userInfoService.checkPhone(phone)){
            throw new BusinessException(MemberCodeMsg.HAS_REGIST_ERROR);
        }
        //注册
        UserInfo userInfo = new UserInfo();
        userInfo.setNickname(nickname);
        userInfo.setPhone(phone);
        userInfo.setEmail("");
        userInfo.setPassword(password);  //假装加密
        userInfo.setGender(UserInfo.GENDER_SECRET);
        userInfo.setLevel(1);
        userInfo.setCity("");
        userInfo.setHeadImgUrl("/images/default.jpg");
        userInfo.setInfo("");

        //核心属性必须自己控制，就算实体类里面有默认值，为了防止意外最好自己添加
        userInfo.setState(UserInfo.STATE_NORMAL);
        //userInfo.setId(null);

        userInfoService.save(userInfo);
        return JsonResult.success();
    }
}